package com.changan.services;

import com.changan.config.ColumnEntity;
import com.changan.config.SqlConfig;
import com.changan.config.TableEntity;
import com.changan.model.SyncTask;
import com.changan.utils.MysqlUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

public class MysqlService {

    public static List<ColumnEntity> getMetadataColumns(Connection conn, String databaseName, String tableName) throws SQLException {
        PreparedStatement preparedStatement = SqlConfig.getMetadataColumns(conn, databaseName, tableName);
        return MysqlUtil.getDatas(preparedStatement, ColumnEntity.class);
    }

    public static List<TableEntity> getMetadataTables(Connection conn, String databaseName) throws SQLException {
        PreparedStatement preparedStatement = SqlConfig.getMetadataTables(conn, databaseName);
        return MysqlUtil.getDatas(preparedStatement, TableEntity.class);
    }

    public static List<TableEntity> getMetadataTable(Connection conn, String databaseName, String tableName) throws SQLException {
        PreparedStatement preparedStatement = SqlConfig.getMetadataTable(conn, databaseName, tableName);
        return MysqlUtil.getDatas(preparedStatement, TableEntity.class);
    }

    //public static List<StartArguments> getJobInfo(Connection conn, Integer id) throws SQLException {
    //    PreparedStatement preparedStatement = SqlConfig.getStartJobInfoDetails(conn, id);
    //    return MysqlUtil.getDatas(preparedStatement, StartArguments.class);
    //}

    public static List<TableEntity> getTableList(Connection connection, SyncTask syncTask) throws SQLException {
        if (syncTask.getTableName() == null) {
            return getMetadataTables(connection, syncTask.getDatabaseName());
        } else {
            return getMetadataTable(connection, syncTask.getDatabaseName(), syncTask.getTableName());
        }
    }
}
